package com.jieyun.rbac.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jieyun.core.exception.ParamException;
import com.jieyun.core.utils.ServerResponse;
import com.jieyun.rbac.constant.RBACConstant;
import com.jieyun.rbac.mapper.SysRoleMapper;
import com.jieyun.rbac.param.SysRoleParam;
import com.jieyun.rbac.po.SysRole;
import com.jieyun.rbac.service.SysRoleService;

/**
 * 系统角色服务
 * 
 * @author trekxu
 *
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Resource
	private SysRoleMapper sysRoleMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jieyun.rbac.service.SysRoleService#addRole(com.jieyun.rbac.param.
	 * SysRoleParam)
	 */
	@Override
	public ServerResponse addRole(SysRoleParam sysRole) {

		// 判断是否存在相同名称的role
		if (existsRoleName(sysRole.getName())) {
			return ServerResponse.fail("已经存在相同名称的角色名称");
		}

		SysRole role = new SysRole();
		BeanUtils.copyProperties(sysRole, role);

		// 添加必要的属性
		role.setStatus(RBACConstant.RoleStatus.ok);
		role.setOperateTime(new Date());
		// TODO
		role.setOperator("trek");

		int count = this.sysRoleMapper.insert(role);
		if (count == 0) {
			return ServerResponse.fail("添加系统角色失败");
		}

		return ServerResponse.ok("添加系统角色成功");
	}

	/**
	 * 判断是否存在相同的roleName
	 * 
	 * @param name
	 * @return
	 */
	private boolean existsRoleName(String name) {
		return this.sysRoleMapper.selectCount(new EntityWrapper<>(SysRole.builder().name(name).build())) == 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jieyun.rbac.service.SysRoleService#deleteRole(java.lang.Integer)
	 */
	@Override
	public ServerResponse roleOnOrOffRole(Integer id) {
		if (id == null) {
			return ServerResponse.fail("被处理的系统角色不能为空");
		}

		SysRole role = this.sysRoleMapper.selectById(id);
		if (role == null) {
			return ServerResponse.fail("被处理的系统角色不存在");
		}

		int status = getStatus(role.getStatus());

		int count = this.sysRoleMapper.updateById(SysRole.builder().id(id).status(status).build());

		if (count == 0) {
			return ServerResponse.fail("操作系统角色失败");
		}

		return ServerResponse.ok("操作系统角色成功");
	}

	/**
	 * 获取相反的状态(激活还是冻结的相反状态)
	 * 
	 * @param status
	 * @return
	 */
	private int getStatus(int status) {
		return status == RBACConstant.RoleStatus.droped ? RBACConstant.RoleStatus.ok : RBACConstant.RoleStatus.droped;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jieyun.rbac.service.SysRoleService#roleUpdate(com.jieyun.rbac.param.
	 * SysRoleParam)
	 */
	@Override
	public ServerResponse roleUpdate(Integer id, String name, String remark) {
		if (id == null) {
			return ServerResponse.fail("更新的系统角色id不能为空");
		}
		if (StringUtils.isBlank(name)) {
			return ServerResponse.fail("更新的系统角色名称不能为空");
		}

		// 先判断否已经存在相同名称的系统该角色了

		SysRole role = this.sysRoleMapper.selectOne(SysRole.builder().name(name).build());
		if (role != null && role.getId() != id) {
			return ServerResponse.fail("已经存在相同名称的角色了");
		}

		// TODO 操作员的名称
		role = SysRole.builder().id(id).name(name).remark(remark).operator("trek").operateTime(new Date()).build();

		int count = this.sysRoleMapper.updateById(role);
		if (count == 0) {
			return ServerResponse.fail("更新系统角色失败");
		}

		return ServerResponse.ok("更新系统角色成功");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jieyun.rbac.service.SysRoleService#roleList(java.lang.Integer)
	 */
	/**
	 * 其中type = 1 表示查询全部 type = 0 表示查询可用的角色
	 */
	@Override
	public List<SysRole> roleList(Integer type) {

		if (type == null) {
			// 查询所有
			return this.sysRoleMapper.selectList(null);
		}

		if (type == 1) {
			return this.sysRoleMapper
					.selectList(new EntityWrapper<>(SysRole.builder().status(RBACConstant.RoleStatus.ok).build()));
		}

		if (type == 0) {
			return this.sysRoleMapper
					.selectList(new EntityWrapper<>(SysRole.builder().status(RBACConstant.RoleStatus.droped).build()));
		}
		
		throw new ParamException("查询的type不符合接口要求");
	}

}
